/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sprites;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;

/**
 *
 * @author ant
 * 
 * Load sprites that will be used by the game and store them as animations.
 */
public class SpriteLoader
{
    public EntityAnimationInstance getAnimation(String name)
    {
        if(!entities.containsKey(name))
            throw new NoSuchElementException("requested animation not available");
        return loadCharacter(entities.get(name));
    }
    
    
    private Map<String, EntityAnimation> entities = new HashMap<>();
    
    //loads character from the default sprite sheet, where each character
    //is itself a 4x3 sprite sheet
    
    public static EntityAnimationInstance loadCharacter(EntityAnimation anim)
    {
        return loadCharacter(anim.ss, anim.tileX, anim.tileY);
    }
    
    public static EntityAnimationInstance loadCharacter(SpriteSheet ss, int tileX, int tileY)
    {
        Animation [] animDirs = new Animation[4];
        int [] dirOrder = {3, 2, 0, 1};
                
        for(int row = tileY; row < 4+tileY; ++row)
        {
            animDirs[dirOrder[row]] = new Animation();
            for(int col = tileX; col < 3+tileX; ++col)
            {
                Animation dir = animDirs[dirOrder[row]];
                Image img = ss.getSubImage(col, row);
                dir.addFrame(img, 10);
                
            }
        }
        
        return new EntityAnimationInstance(animDirs, ss);

    }
    
    public SpriteLoader() throws SlickException
    {
        //load the animations from the defautl sprite sheet.
        
        SpriteSheet sheet = new SpriteSheet("assets/sprites/Doll_1.png", 36, 36);
        
        
        entities.put("alice", new EntityAnimation(sheet, 0,0));
        entities.put("sakuya", new EntityAnimation(sheet, 3,0));
        entities.put("ran", new EntityAnimation(sheet, 6,0));
        entities.put("chen", new EntityAnimation(sheet, 9,0));
    }
}
